package com.mobile.collect.beans;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile.collect.R;
import com.mobile.collect.dialogs.BottomListDialog;
import com.mobile.collect.view.DescDialog;
import com.mobile.collect.view.InputDialog;

import java.util.ArrayList;
import java.util.List;

public class PointGroupViewInfo implements View.OnClickListener {

    private TextView descTv;
    private TextView highTv;
    private TextView keepWarmTv;
    private LinearLayout childLayout;
    TextView hardTv;
    EditText hardeasonTv;
    TextView dangerTv;
    TextView statusTv;
    View box_hard_remark;
    View mView;
    ImageView hardeasonIv;

    public PointGroupViewInfo(View view) {
        this.mView = view;
    }

    List<PointViewInfo> list;

    public TextView getDescTv() {
        return descTv;
    }

    public View getmView() {
        return mView;
    }

    public PointGroupViewInfo setmView(View mView) {
        return setmView(mView, "", "", "", "", "", "", "");
    }

    public PointGroupViewInfo setmView(View mView, String status, String high, String checkHardType, String checkHardReason, String dangerCheck, String keepWarm, String AdditionalDes) {
//    public PointGroupViewInfo setmView(View mView, DevicePointInfo pointInfo) {
        this.mView = mView;
        descTv = mView.findViewById(R.id.descTv);
        highTv = mView.findViewById(R.id.highTv);
        keepWarmTv = mView.findViewById(R.id.keepWarmTv);
        hardTv = mView.findViewById(R.id.hardTv);
        hardeasonTv = mView.findViewById(R.id.hardeasonTv);
        statusTv = mView.findViewById(R.id.statusTv);
        box_hard_remark = mView.findViewById(R.id.box_hard_remark);
        dangerTv = mView.findViewById(R.id.dangerTv);
        childLayout = mView.findViewById(R.id.childLayout);
        ImageView descMoreIv = mView.findViewById(R.id.descMoreIv);
        hardeasonIv = mView.findViewById(R.id.hardeasonIv);
        highTv.setOnClickListener(this);
        keepWarmTv.setOnClickListener(this);
        hardTv.setOnClickListener(this);
        statusTv.setOnClickListener(this);
        dangerTv.setOnClickListener(this);
        descMoreIv.setOnClickListener(this);
        hardeasonIv.setOnClickListener(this);
        setHardTextChangedListener();

        statusTv.setText(status);

        if (!TextUtils.isEmpty(checkHardType)) {
            hardTv.setText(checkHardType);
        } else {
            hardTv.setText("否");
        }

        if (TextUtils.equals("是", checkHardType)) {
            hardeasonTv.setVisibility(View.VISIBLE);
        }
        hardeasonTv.setText(checkHardReason);

        if (!TextUtils.isEmpty(dangerCheck)) {
            dangerTv.setText(dangerCheck);
        } else {
            dangerTv.setText("否");
        }
        if (!TextUtils.isEmpty(keepWarm)) {
            keepWarmTv.setText(keepWarm);
        } else {
            keepWarmTv.setText("否");
        }

        highTv.setText(high);
        descTv.setText(AdditionalDes);
        return this;
    }

    public void setDescTv(TextView descTv) {
        this.descTv = descTv;
    }

    public TextView getHighTv() {
        return highTv;
    }

    public void setHighTv(TextView highTv) {
        this.highTv = highTv;
    }

    public TextView getKeepWarmTv() {
        return keepWarmTv;
    }

    public void setKeepWarmTv(TextView keepWarmTv) {
        this.keepWarmTv = keepWarmTv;
    }

    public TextView getHardTv() {
        return hardTv;
    }

    public void setHardTv(TextView hardTv) {
        this.hardTv = hardTv;
    }

    public TextView getHardeasonTv() {
        return hardeasonTv;
    }

    public void setHardeasonTv(EditText hardeasonTv) {
        this.hardeasonTv = hardeasonTv;
    }

    public TextView getDangerTv() {
        return dangerTv;
    }

    public void setDangerTv(TextView dangerTv) {
        this.dangerTv = dangerTv;
    }

    public TextView getStatusTv() {
        return statusTv;
    }

    public void setStatusTv(TextView statusTv) {
        this.statusTv = statusTv;
    }

    public List<PointViewInfo> getList() {
        return list;
    }

    public void setList(List<PointViewInfo> list) {
        this.list = list;
    }


    private void setHardTextChangedListener() {
        hardTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String inputStr = s.toString();
                    if (TextUtils.isEmpty(inputStr)) {
                        box_hard_remark.setVisibility(View.GONE);
                    } else {
                        if (inputStr.equals("是")) {
                            box_hard_remark.setVisibility(View.VISIBLE);
                        } else {
                            box_hard_remark.setVisibility(View.GONE);
                            hardeasonTv.setText("");
                        }
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.statusTv:
                showYesNoDialog(statusTv);
                break;
            case R.id.highTv:
                showHighDialog();
                break;
            case R.id.keepWarmTv:
                showYesNoDialog(keepWarmTv);
                break;
            case R.id.hardTv:
                showYesNoDialog(hardTv);
                break;
            case R.id.dangerTv:
                showYesNoDialog(dangerTv);
                break;
            case R.id.descMoreIv:
                showDescDialog();
                break;
            case R.id.hardeasonIv:
                showYesNoDialog(hardeasonTv);
                break;


        }
    }

    private void showDescDialog() {
        DescDialog numberPop = new DescDialog(mView.getContext(), "附加描述", new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                descTv.append(value);
            }
        });
        numberPop.show();
    }

    private void showHighDialog() {
        InputDialog numberPop = new InputDialog(mView.getContext(), "高度", new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                highTv.setText(value);
                if (Float.parseFloat(value) >= 5) {
                    hardTv.setText("是");
                    box_hard_remark.setVisibility(View.VISIBLE);
                }
            }
        }, 0);
        numberPop.disPlay(InputDialog.broad2);

    }

    private void showYesNoDialog(final TextView clickView) {
        List<String> list = new ArrayList<>();
        switch (clickView.getId()) {
            case R.id.keepWarmTv:
            case R.id.hardTv:
            case R.id.dangerTv:
                list.add("是");
                list.add("否");
                break;
            case R.id.statusTv:
                list = new ArrayList<>();
                list.add("轻液");
                list.add("重液");
                list.add("气体/蒸汽");
                break;
            case R.id.hardeasonTv:
                list = new ArrayList<>();
                list.add("密封点所在部位超出操作人员触及范围2米以上");
                break;
        }
        BottomListDialog listDialog = new BottomListDialog(mView.getContext(), new BottomListDialog.BottomListSelectListener() {
            @Override
            public void itemClickCallback(String selectStr) {
                clickView.setText(selectStr);
                if (clickView.getId() == R.id.hardTv) {
                    setReleven(selectStr);
                }
            }
        });
        listDialog.disPlay(list);
    }

    private void setReleven(String txt) {
        if (txt.equals("是")) {
            box_hard_remark.setVisibility(View.VISIBLE);
            hardeasonTv.setText("密封点所在部位超出操作人员触及范围2米以上");
        } else {
            box_hard_remark.setVisibility(View.GONE);
            hardeasonTv.setText("");
        }
    }

    public void addPointView(View view) {
        childLayout.addView(view);
    }

    public void remove(int position) {
        list.remove(position);
        childLayout.removeViewAt(position);
    }


    public void addPointView(PointViewInfo info, int position) {
        list.add(position, info);
        childLayout.addView(info.getmView(), position);
    }
}
